package com.cqkjxy.commons.dao;


import com.cqkjxy.commons.constants.EntityStatus;
import com.cqkjxy.commons.entities.AbstractStatefulEntity;
import com.cqkjxy.commons.repositories.AbstractStatefulRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

public abstract class AbstractStatefulEntityDao<T extends AbstractStatefulEntity, R extends AbstractStatefulRepository<T>>
        extends AbstractEntityDao<T, R>{

    public List<T> findAllEnabled() {
        if (getDefaultSorting() != null) {
            return getRepository().findByStatus(EntityStatus.NORMAL, getDefaultSorting());
        }
        return getRepository().findByStatus(EntityStatus.NORMAL);
    }

    public List<T> findAllDisabled() {
        if (getDefaultSorting() != null) {
            return getRepository().findByStatus(EntityStatus.DISABLED, getDefaultSorting());
        }
        return getRepository().findByStatus(EntityStatus.DISABLED);
    }

    public Page<T> findAllEnabled(Pageable pageable) {
        Pageable newPageable = addDefaultSortToPageable(pageable);
        return getRepository().findByStatus(EntityStatus.NORMAL, newPageable);
    }

    public Page<T> findAllDisabled(Pageable pageable) {
        Pageable newPageable = addDefaultSortToPageable(pageable);
        return getRepository().findByStatus(EntityStatus.DISABLED, newPageable);
    }


}